<template>
  <div class="login">
    <div class="box"></div>
    <div class="box3">
      <el-col class="box4">
        <el-avatar :size="75" :src="circleUrl"></el-avatar>
      </el-col>
      <el-form
        :model="chongzhi"
        :rules="loginFormRules"
        label-width="100px"
        class="box2"
      >
        <el-form-item label="账号" prop="userCode" style="width: 400px">
          <el-input v-model="chongzhi.userCode"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" style="width: 400px">
          <el-input v-model="chongzhi.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <!-- <el-button @click="sjdl()">商家登录</el-button>
          <el-button @click="glydl()">管理员登录</el-button> -->
          <el-button @click="submit()">登录</el-button>
           <el-button @click="cz()">重置</el-button>
        </el-form-item>
        <!-- <el-form-item>
         
          <el-button @click="yhzc()">用户注册</el-button>
        </el-form-item> -->
      </el-form>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form :model="user">
        <el-form-item label="姓名" style="width: 300px">
          <el-input v-model="user.userCode"></el-input>
        </el-form-item>
        <el-form-item label="年龄" style="width: 300px">
          <el-input v-model="user.age" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="电话" style="width: 300px">
          <el-input v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item label="家庭住址" style="width: 300px">
          <el-input v-model="user.address"></el-input>
        </el-form-item>

        <el-form-item
          label="用户名"
          style="width: 300px"
          v-if="action == 'create'"
        >
          <el-input v-model="user.userCode"></el-input>
        </el-form-item>
        <el-form-item label="密码" style="width: 300px">
          <el-input v-model="user.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="性别" style="width: 300px">
          <el-select v-model="user.sex">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        <el-button @click="quxiao">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      dialogTitle: "",
      user: [],
      action: "",
      dialogFormVisible: false,
      circleUrl: require("@/assets/002.png"),
      chongzhi: {},
      loginFormRules: {
        userCode: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    yhzc() {
      this.dialogTitle = "用户注册";
      this.dialogFormVisible = true;
      this.user = {};
    },
    quxiao() {
      this.dialogFormVisible = false;
      location.reload();
    },
    addOrUpdate() {
      this.$axios
        .post("http://localhost:8000/user/create", this.user)
        .then((res) => {
          this.dialogFormVisible = false;
          location.reload();
        });
    },
    glydl() {
      this.$axios
        .post("http://localhost:8000/shop/login", this.chongzhi)
        .then((res) => {
          if (res.data.code === 200) {
            console.log(res.data.code);
            window.sessionStorage.setItem("token", res.data.obj);
            localStorage.setItem("token", res.data.obj);
            this.action = "admin";
            window.sessionStorage.setItem("action", this.action);
            this.$router.push("/welcome");
          } else if (res.data.code === 70001) {
            alert("登录失败，账号或密码错误");
          } else {
            alert("用户名不存在");
          }
        });
    },
    sjdl() {
      this.$axios
        .post("http://localhost:8000/dish/login", this.chongzhi)
        .then((res) => {
          if (res.data.code === 200) {
            window.sessionStorage.setItem("token", res.data.obj);
            localStorage.setItem("token", res.data.obj);
            this.action = "shop";
            window.sessionStorage.setItem("action", this.action);
            this.$router.push("/welcome");
          } else if (res.data.code === 70001) {
            alert("登录失败，账号或密码错误");
          } else {
            alert("用户名不存在");
          }
        });
    },
    submit() {
      axios.post("http://localhost:8000/login", this.chongzhi).then((res) => {
        if (res.data.code === 200) {
          console.log(res.data.code);
          window.sessionStorage.setItem("token", res.data.obj);
          localStorage.setItem("token", res.data.obj);
          this.action = "user";
          window.sessionStorage.setItem("action", this.action);
          this.$router.push("/welcome");
        } else if (res.data.code === 70001) {
          alert("登录失败，账号或密码错误");
        } else {
          alert("用户名不存在");
        }
      });
    },
    cz() {
      this.chongzhi = {};
    },
  },
};
</script>

<style>
.box {
  background-image: url(./assets/01.jpg);
  background-size: 1920px, 1080px;
  width: 1920px;
  height: 1080px;
  margin-top: -50px;
  margin: 0 auto;
}
.box2 {
  margin-top: 30px;
}
.box3 {
  background-color: rgb(29, 33, 77);
  height: 300px;
  width: 500px;
  margin-left: 35%;
  margin-top: -800px;
  color: rgb(32, 224, 32);
  box-sizing: border-box;
}
.box4 {
  margin-top: -30px;
  background-size: 50px;
}
.login {
  background-size: 1920px, 1080px;
  width: 1920px;
  height: 1080px;
  margin: 0 auto;
  margin-top: -50px;
}
</style>